package top.swimmer.tokenizer.dictionary;

import top.swimmer.tokenizer.dictionary.trie.DoubleArrayTrieJ;
import top.swimmer.tools.io.IOUtils;

import java.util.ArrayList;
import java.util.List;

public class DoubleArrayTrieDictionary extends AbstractDictionary {
    private DoubleArrayTrieJ trie;

    @Override
    public boolean contains(String key) {
        return trie.contains(key);
    }

    @Override
    public boolean contains(String key, int start, int length) {
        return trie.contains(key, start, length);
    }

    @Override
    protected void clear() {
        trie = null;
    }

    @Override
    protected void load() {
        List<String> words = new ArrayList<>();
        IOUtils.readFromResource("/dictionary/dic.txt", words::add);
        trie = new DoubleArrayTrieJ();
        trie.build(words);
        words = null;
    }
}
